
def variable_name(x):
	if '.' in x:
		l = ''.join(x.split('.')[1:])
	else:
		l = x
	l = l.strip()		
	if l.startswith('get_') or l.startswith('set_'):
		l = l[4:]
	return l.strip()

def category(x):
	if '.' in x:
		l = x.split('.')[0]
	else:
		l = 'global'

	return l.strip()
	
def typeof(x):
	if 'get_' in x: 
		l = 'get'
	elif 'set_' in x:
		l = 'set'
	else:
		l = ''
	return l

class Variable:
	def __init__(self, name):
		self.name = name.strip()
		self.variable_name = variable_name(self.name)
		self.category = category(self.name)
		self.typeof = typeof(self.name)


	def __add__(self, w):
		print "dupa" 

	def __cmp__(self, c):
		r = cmp(self.category,c.category)
		if r == 0:
			r = cmp(self.name,c.name) 
		return r			
	
	def __str__(self):
		return "<XF %s - %s>" % (self.name, self.variable_name)

	def to_str(self):
		return "%-30s %-50s %-10s %-5s" % (self.name, self.variable_name, self.category, self.typeof)

	def to_python(self):
		get_function  = self.name.replace('set_', 'get_')
		set_function  = self.name.replace('get_', 'set_')
		if self.typeof == 'setget':
			S = """    %-19s = property( MWT()( lambda self: self.server.%s(self.torrent_id)), lambda self: self.server.%s(self.torrent_id))""" % (self.variable_name, get_function, set_function)
		elif self.typeof == 'get':			
			S = """    %-19s = property(MWT()( lambda self: self.server.%s(self.torrent_id)))""" % (self.variable_name, get_function)
		elif self.typeof == 'set':		
			S = """    def set_%s(self, v): self.server.%s(v)""" % (self.variable_name, self.name)
#			S = """    def """ #%-19s = property(MWT()( lambda self: self.server.%s(self.torrent_id)))""" % (self.variable_name, get_function)
		else:
			S = """    # defines %s""" % self.name
		S += "   # defined by %s - %s" % (self.name, self.variable_name)			
		return S
		#S = """    %-19s = property(MWT()(lambda self: xmlrpc_call(self.server.%s, self, self.torrent_id)))"""

		return 
from UserDict import DictMixin

class Variables(DictMixin):

	def __init__(self):
		self.d = {}

	def __setitem__(self, key, val):
		key = variable_name(key)
		if key not in self.d:
			self.d[key] = val
		else:
#			print key
			if (self.d[key].typeof == 'set' and val.typeof == "get") or \
			   (self.d[key].typeof == 'get' and val.typeof == "set"):
				self.d[key].typeof = 'setget'

	def __getitem__(self, key):
		return self.d[key]
	def keys(self):
		return self.d.keys()

vs = Variables()
for x in open('doc/xmlrpc.txt').readlines()[5:]:
	vs[x] = Variable(x)
print "\n"*10
print "="*50
print "PLikow"
for x in sorted(vs.values()):
	if x.category in ["d", "f", "t"]:
		print x.to_python()
print "="*50
print "Globl"
for x in sorted(vs.values()):
	if x.category not in ["d", "f", "t"]:
		print x.to_python()

#	line = x.strip()
